package com.kakarote.examine.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.kakarote.core.common.enums.FieldEnum;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.feign.crm.entity.ExamineField;
import com.kakarote.core.feign.examine.entity.ExamineFlowFieldUpdateLogBO;
import com.kakarote.core.feign.examine.entity.ExamineUpdateFieldBO;
import com.kakarote.examine.constant.ExamineCodeEnum;
import com.kakarote.examine.entity.PO.ExamineFlowAuthField;
import com.kakarote.examine.entity.PO.ExamineFlowFieldUpdateLog;
import com.kakarote.examine.service.ExamineModuleService;
import com.kakarote.examine.service.IExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author JiaS
 * @date 2020/12/22
 */

@Service("examineJxcService")
public class ExamineJxcServiceImpl implements ExamineModuleService {

    @Autowired
    private IExamineService examineService;


    @Override
    public List<ExamineField> queryExamineField(Integer label, Long categoryId) {
        int five = 5;
        int six = 6;
        int seven = 7;
        int eight = 8;
        //进销存原有接口使用的标识不一致 部分需要转换
        if (ListUtil.toList(five, six, seven, eight).contains(label)) {
            label = label - 2;
        }
        List<ExamineField> examineFields = Collections.emptyList();
        if (CollectionUtil.isNotEmpty(examineFields)) {
            for (ExamineField examineField : examineFields) {
                examineField.setFieldName(StrUtil.toCamelCase(examineField.getFieldName()));
            }
        }
        return examineFields;
    }

    @Override
    public void updateCheckStatus(Integer label, Long typeId, Integer checkStatus, Object data) {
    }

    @Override
    public void checkStatus(Integer label, Long typeId, Integer checkStatus, Integer oldCheckStatus) {
        int four = 4;
        if (checkStatus == four) {
            //当前审核已通过不可撤回
            if (oldCheckStatus == 1) {
                throw new CrmException(ExamineCodeEnum.EXAMINE_RECHECK_PASS_ERROR);
            }
        }
    }

    @Override
    public Map<String, Object> getConditionMap(Integer label, Long typeId, Long recordId) {
        Map<String, Object> map = new HashMap<>(4);
        return map;
    }

    /**
     * 获取成员组件需要的数据entity
     *
     * @param fieldName
     * @param label
     * @param typeId
     * @param recordId
     * @return
     */
    @Override
    public Map<String, Object> getMemberComponentMap(String fieldName, Integer label, Long typeId, Long recordId) {
        //审批Label转为业务label
        if (label == 5) {
            label = 3;
        } else if (label == 6) {
            label = 4;
        } else if (label == 7) {
            label = 5;
        } else if (label == 8) {
            label = 6;
        } else if (label == 9) {
            label = 7;
        } else if (label == 10) {
            label = 8;
        } else if (label == 11) {
            label = 9;
        } else if (label == 12) {
            label = 10;
        }
        Map<String, Object> beanMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>(4);
        map.put(fieldName, beanMap.get(fieldName));
        return map;
    }


    @Override
    public List<?> getAuthField(List<ExamineFlowAuthField> authFields, Integer label, Long typeId, List<ExamineFlowFieldUpdateLog> updateLogs) {
        List<Map<String, Object>> data = Collections.emptyList();
        if (CollectionUtil.isNotEmpty(authFields)) {
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, ExamineFlowAuthField> collect = authFields.stream().filter(authField -> authField.getAuthLevel() > 1).collect(Collectors.toMap(
                    e -> StrUtil.toCamelCase(e.getFieldName()), Function.identity(), (k1, k2) -> k1
            ));
            Set<String> keySet = collect.keySet();

            for (Map<String, Object> datum : data) {
                if (keySet.contains(datum.get("fieldName"))) {
                    Object type = datum.get("type");
                    ExamineFlowAuthField authField = collect.get(datum.get("fieldName"));
                    datum.put("authLevel", authField.getAuthLevel());
                    datum.put("tableLevel", authField.getTableLevel() == null ? 0 : authField.getTableLevel());
                    if (FieldEnum.DETAIL_TABLE.getType().equals(type)) {
                        disposeDetailTable(datum, authField.getFieldExtendList());
                    }
                    list.add(datum);
                }
            }

            for (ExamineFlowFieldUpdateLog updateLog : updateLogs) {
                String fieldName = updateLog.getFieldName();
                String newValue = updateLog.getNewValue();
                for (Map<String, Object> datum : list) {
                    if (fieldName.equals(datum.get("fieldName"))) {
                        datum.put("value", convertValueByFormType(newValue, updateLog.getType()));
                    }
                }
            }
            return list;
        }
        data.forEach(map -> {
            map.put("authLevel", 2);
            Object type = map.get("type");
            if (FieldEnum.DETAIL_TABLE.getType().equals(type)) {
                map.put("tableLevel", 0);
            }
        });
        return data;
    }

    @Override
    public void updateFieldInfo(ExamineUpdateFieldBO examineUpdateFieldBO) {

    }

    @Override
    public List<Map<String, Object>> queryExamineUpdateLog(List<ExamineFlowFieldUpdateLogBO> list) {
        return  Collections.emptyList();
    }
}
